const fs = require('fs');
const { createCanvas } = require('canvas');

// 检查是否安装了canvas模块
try {
    require('canvas');
} catch (error) {
    console.log('需要安装canvas模块，请运行: npm install canvas');
    console.log('或者使用浏览器版本的图标生成器: images/icon-generator.html');
    process.exit(1);
}

// 创建images目录
if (!fs.existsSync('images')) {
    fs.mkdirSync('images');
}

// 图标配置
const icons = [
    { name: 'home.png', emoji: '🏠', color: '#9CA3AF' },
    { name: 'home-active.png', emoji: '🏠', color: '#8B5CF6' },
    { name: 'order.png', emoji: '📋', color: '#9CA3AF' },
    { name: 'order-active.png', emoji: '📋', color: '#8B5CF6' },
    { name: 'profile.png', emoji: '👤', color: '#9CA3AF' },
    { name: 'profile-active.png', emoji: '👤', color: '#8B5CF6' }
];

// 生成图标
icons.forEach(icon => {
    const canvas = createCanvas(81, 81);
    const ctx = canvas.getContext('2d');
    
    // 白色背景
    ctx.fillStyle = '#FFFFFF';
    ctx.fillRect(0, 0, 81, 81);
    
    // 圆形背景
    ctx.fillStyle = icon.color;
    ctx.beginPath();
    ctx.arc(40.5, 40.5, 30, 0, 2 * Math.PI);
    ctx.fill();
    
    // emoji图标
    ctx.font = '40px Arial';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillStyle = '#FFFFFF';
    ctx.fillText(icon.emoji, 40.5, 40.5);
    
    // 保存为PNG
    const buffer = canvas.toBuffer('image/png');
    fs.writeFileSync(`images/${icon.name}`, buffer);
    
    console.log(`✅ 生成图标: ${icon.name}`);
});

console.log('\n🎉 所有图标生成完成！');
console.log('现在您可以重新启动微信开发者工具了。');
console.log('\n图标文件说明:');
console.log('- home.png / home-active.png: 首页图标');
console.log('- order.png / order-active.png: 订单图标');
console.log('- profile.png / profile-active.png: 我的图标');
